﻿using System.Collections.ObjectModel;
using System.Linq;
using System.Windows.Controls;
using System.Windows.Input;
using Microsoft.Expression.Interactivity.Core;
using QDP.Platform.Services;
using QDP.Platform.Silverlight;
using QDP.Platform.Silverlight.Controls;
using Services.Works;
namespace Apps.WorkPlugin
{
    public class WorkMonitor : PartElement
    {
        public ObservableCollection<MonitorData> Monitors { get; private set; }
        public ObservableCollection<MonitorData> ToWork
        {
            get
            {
                var res = new ObservableCollection<MonitorData>();
                var items = (from work in Monitors where work.Mode == WorkMode.ToWork select work);
                foreach (var item in items)
                    res.Add(item);
                return res;
            }
        }
        public ObservableCollection<MonitorData> Worked
        {
            get
            {
                var res = new ObservableCollection<MonitorData>();
                var items = (from work in Monitors where work.Mode == WorkMode.Worked select work);
                foreach (var item in items)
                    res.Add(item);
                return res;
            }
        }
        public ObservableCollection<MonitorData> Working
        {
            get
            {
                var res = new ObservableCollection<MonitorData>();
                var items = (from work in Monitors where work.Mode == WorkMode.Working select work);
                foreach (var item in items)
                    res.Add(item);
                return res;
            }
        }
        public ICommand ViewCommand { get; private set; }
        public ICommand ShowInToolbarCommand { get; private set; }
        Grid LayoutRoot;
        private MonitorView monitorView;
        private bool ShowView = false;

        public WorkMonitor()
        {
            Monitors = new ObservableCollection<MonitorData>()
            {
                new MonitorData(){Mode= WorkMode.ToWork, Title="需要添加一个新用户",Type="add",WidgetCaption="用户",WidgetCode="YunWei.Basic.Account"},
                new MonitorData(){Mode= WorkMode.Worked,Title="用户yu_hui需要更新资料",Id="yu_hui",Type="edit",WidgetCaption="用户",WidgetCode="YunWei.Basic.Account"},
                new MonitorData(){Mode= WorkMode.Working,Title="打开用户应用",WidgetCaption="用户",WidgetCode="YunWei.Basic.Account"},
                new MonitorData(){Mode= WorkMode.ToWork, Title="打开角色管理应用",WidgetCaption="角色",WidgetCode="YunWei.Basic.Role"},
            };
            LayoutRoot = new Grid()
            {
                Width = 80,
            };
            monitorView = new MonitorView()
            {
                DataContext = Monitors,
                SelectionChanged = SelectionChanged
            };
            LayoutRoot.Children.Add(monitorView);

            ViewCommand = new ActionCommand(this.view);
            ShowInToolbarCommand = new ActionCommand(this.ShowInToolbar);
        }

        private void view()
        {
            Monitors.Add(
                new MonitorData() { Title = "工作组需要更新信息", WidgetCaption = "工作组", WidgetCode = "YunWei.Basic.Group" });
        }
        private void ShowInToolbar()
        {
            if (ShowView)
            {
                Q.ToolbarManager.Remove(LayoutRoot);
                ShowView = false;
            }
            else
            {

                Q.ToolbarManager.Add(LayoutRoot, ToolbarLayoutDirection.Custom);
                ShowView = true;
            }
        }

        private void SelectionChanged(object selected)
        {
            MonitorData data = selected as MonitorData;
            //Q.MessageManager.Show(data.Title);
            Q.WidgetManager.GetWidget(data.WidgetCode, (s) =>
            {
                switch (s.State)
                {
                    case LoadActionState.Complated:
                        var element = s.Element;
                        // 打开窗口
                        if (element.IsShowed == false)
                            element.Show();
                        // 激活窗口
                        if (element.IsActived == false)
                            element.Active();

                        if (element.WidgetContent is WidgetControl)
                        {
                            // 用于恢复窗口大小
                            var widget = element.WidgetContent as WidgetControl;
                            if (widget.IsMinimized) widget.OnRestore();
                            // 指定窗口指令
                            if ((element.WidgetContent as WidgetControl).Child is IWorkView)
                            {
                                var view = (element.WidgetContent as WidgetControl).Child as IWorkView;
                                view.ToDo(data.Type, data.Id);
                            }
                        }
                        else if (element.WidgetContent is IWorkView)
                        {
                            var view = element.WidgetContent as IWorkView;
                            view.ToDo(data.Type, data.Id);
                        }
                        break;
                }
            });
        }

    }

    public class MonitorData
    {
        public string Title { get; set; }
        public string Type { get; set; }
        public string Id { get; set; }
        public string WidgetCaption { get; set; }
        public string WidgetCode { get; set; }
        public WorkMode Mode { get; set; }
    }
    public enum WorkMode
    {
        ToWork, Worked, Working
    }
}